package task5_11_01;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/27 8:40
 * @description:
 */
public class Task5_11_01 {
  public static void main(String[] args) {
    //1.创建hashmap对象
    HashMap<String, Student> studentHashMap = new HashMap<String, Student>();
    //2.添加元素
    Student l1 = new Student("zhangsan", 18);
    Student l2 = new Student("lisi", 19);
    Student l3 = new Student("wangwu", 20);
    studentHashMap.put("s1", l1);
    studentHashMap.put("s2", l2);
    studentHashMap.put("s3", l3);
    //3.遍历
    Iterator iterator = studentHashMap.keySet().iterator();
    while (iterator.hasNext()) {
      while (iterator.hasNext()){
        //每一个键
        String key= (String) iterator.next();
        //通过键得到值
        Student value=studentHashMap.get(key);
        System.out.println(key+":"+value);
      }


      //通过键值对集合来获取键和值
      Iterator iterator1= studentHashMap.entrySet().iterator();
      while (iterator1.hasNext()) {
        //每一个键值对
        Map.Entry<String,Student> entry= (Entry<String,Student>) iterator1.next();
        //通过键值对获取键和值
        String key=entry.getKey();
        Student value=entry.getValue();
        System.out.println(key+"--"+value);
      }
    }
  }
}
